var base_url = 'http://localhost/wchart/Chart/';

function ajaxDataRenderer(url) {
    var ret = null;
    $.ajax({
      // have to use synchronous here, else the function 
      // will return before the data is fetched
      async: false, cache: false, url: url, dataType:"json",
      success: function(data) {
      	if (data.success) {
	        ret = data;
      	}
      }
    });
    return ret;
};

function ajaxDataRenderer_ko(url) {
    var ret = null;
    $.post(url,function(data) {
      	if (data.success) {
	        ret = data;
      	}
      }
      ,'json');
    return ret;
};

function loadScript(url, callback){

    var script = document.createElement("script")
    script.type = "text/javascript";

    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function(){
            callback();
        };
    }

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

function loadCss(url) {
	// <link rel="stylesheet" type="text/css" href="/minijob/css/bootstrap.min.css" />
    var css = document.createElement("link")
    css.type = "text/css";
    css.rel="stylesheet"
    css.href = url;
    document.getElementsByTagName("head")[0].appendChild(css);
}

function drawChart(div_name, chart_code, chart_title) {
	data = ajaxDataRenderer(base_url+'draw/'+chart_code);
	if (chart_title==null)
		chart_title=data.title;
	switch(data.type)
	{
		case 'L': //linear
			  $.jqplot(div_name, data.data,{
			    title: chart_title
				, grid: {
					// borderColor:'transparent',drawBorder:false,shadowColor:'transparent',
				    drawGridLines: true,        // wether to draw lines across the grid or not.
				    gridLineColor: '#cccccc',   // *Color of the grid lines.
				    background: '#ffffff',      // CSS color spec for background color of grid.
				    borderColor: '#cccccc',     // CSS color spec for border around grid.
				    borderWidth: 1.0,           // pixel width of border around grid.
				    shadow: false,               // draw a shadow for grid.
				    shadowAngle: 45,            // angle of the shadow.  Clockwise from x axis.
				    shadowOffset: 1.5,          // offset from the line of the shadow.
				    shadowWidth: 3,             // width of the stroke for the shadow.
				    shadowDepth: 3,             // Number of strokes to make when drawing shadow.
				                                // Each stroke offset by shadowOffset from the last.
				    shadowAlpha: 0.07,          // Opacity of the shadow
				    renderer: $.jqplot.CanvasGridRenderer,  // renderer to use to draw the grid.
				    rendererOptions: {}         // options to pass to the renderer.  Note, the default
				                                // CanvasGridRenderer takes no additional options.
				}
				, axes:{yaxis:{min:0, max:50}}
			  });
		  break;
		case 'P': //pie
  			$.jqplot (div_name, data.data,{
					    title: chart_title
      					,seriesDefaults: {
					        // Make this a pie chart.
					        renderer: jQuery.jqplot.PieRenderer, 
					        rendererOptions: {
					          // Put data labels on the pie slices.
					          // By default, labels show the percentage of the slice.
					          showDataLabels: true
					        }
			      		}
						,grid: {
							borderColor:'transparent',drawBorder:false,shadowColor:'transparent',
						    drawGridLines: true,        // wether to draw lines across the grid or not.
						    gridLineColor: '#cccccc',   // *Color of the grid lines.
						    background: '#ffffff',      // CSS color spec for background color of grid.
						    borderColor: '#cccccc',     // CSS color spec for border around grid.
						    borderWidth: 1.0,           // pixel width of border around grid.
						    shadow: false,               // draw a shadow for grid.
						    shadowAngle: 45,            // angle of the shadow.  Clockwise from x axis.
						    shadowOffset: 1.5,          // offset from the line of the shadow.
						    shadowWidth: 3,             // width of the stroke for the shadow.
						    shadowDepth: 3,             // Number of strokes to make when drawing shadow.
						                                // Each stroke offset by shadowOffset from the last.
						    shadowAlpha: 0.07,          // Opacity of the shadow
						    renderer: $.jqplot.CanvasGridRenderer,  // renderer to use to draw the grid.
						    rendererOptions: {}         // options to pass to the renderer.  Note, the default
						                                // CanvasGridRenderer takes no additional options.
						}
				      	,legend: { show:true, location: 'e' }
			    });
		break;
		default:
	}
	
}

